// ignore_for_file: prefer_typing_uninitialized_variables

import 'dart:io';
import 'package:commons/commons.dart';
import 'package:dio/io.dart';
import 'package:dio/dio.dart';

class ServiceConfig {
  ServiceConfig(this.dio);

  factory ServiceConfig.create(Map<String, dynamic> json) {
    final dio = Dio(
      BaseOptions(
        connectTimeout: Duration(seconds: 5),
        receiveTimeout: Duration(seconds: 15),
      ),
    );
    var adapter = (dio.httpClientAdapter as IOHttpClientAdapter);
    adapter.createHttpClient = () {
      var client = HttpClient();
      client.badCertificateCallback = (
        X509Certificate cert,
        String host,
        int port,
      ) {
        return true;
      };
      return client;
    };
    return ServiceConfig(dio).._load(json);
  }

  void _load(Map<String, dynamic> json) async {
    baseUrl = json.getStr('baseUrl');
    imgUrl = json.getStr('imgUrl');
    channel = json.getStr('channel');
    runAppModel = json.getStr('runAppModel');
  }

  Dio dio;
  bool get isProd => runAppModel == 'prod';
  late final baseUrl;
  late final imgUrl;
  late final channel;
  late final runAppModel;
}
